<?php 
namespace app\index\Controller;
use think\Controller;
use think\Db;//导入数据库类 (DB类)


class Content  extends Controller
{

    
    public function index()
    {
        //文章内容显示
        $art_id=input('art_id');
        // dump( $art_id);exit;
        $list = db('article')->find($art_id);
       
        $res = db('session')->where('id',$list['ses_id'])->find();
        if ($res>0) {
            $this->assign('list',$list);
            $this->assign('res',$res);

        }else{
            return view('404/404');
        }
       
        

        //发帖人信息
        $user = db('user')->find($list['use_id']);
        $this->assign('user',$user);
        // var_dump($user);

        //作者的上一篇信息

        //评论
     
      $reply=Db::field('reply.id,reply.art_id,reply.ses_id,reply.use_id,reply.content,reply.time,user.tel,user.nickname,user.icon,user.regtime,user.diary,user.photo,user.sightml,user.ft_count')
      ->table('bbs_reply reply,bbs_user user')
      ->where('user.id=reply.use_id')
      ->where('reply.art_id='.$art_id)
      ->select();
      // var_dump($reply);exit;
      $this->assign('reply',$reply);


      //文章浏览数
      $check=db('article')->find($art_id);
      $check=$check['check']+1;
      db('article')->where('id',$art_id)->setField('check', $check);
        
      return $this->fetch('Content/content');
    
    }
    //发帖并自动跳转文章页;
    public function postSubmit()
    {
      if (empty(input('session.user.id'))) {
        $this->error('登陆后才能执行操作哦','/index/User/login');
      }
       
        $title=input('title');
        $content=input('content');
       
        $uid=input('uid');
        $sid=input('sid');      //session里的用户id
        $ses_id=input('ses_id');//板块ID
        $uname=input('uname');
        preg_match_all("/\<img.*?src\=\"(.*?)\"[^>]*>/i", $content, $match);
     
        if (!empty($match[1])) {
         $image=$match[1][0];
           
         }else{
          $image='/static/images/none1.gif';
        }
        
       if (empty($title&&$content)) {
         $this->error('标题或内容不能为空');
       }
        $post=["ses_id"=>$uid,"author"=> $uname,"use_id"=>$sid,"check"=>1,"count"=>0,"title"=>$title,"content"=>$content,'pubdate'=>time(),'image'=>$image];
        $res=db('article')->insert($post);
        $art=db('article')->where("ses_id",$uid)->order('id desc')->select();
        $art_id=$art[0]['id'];

      
       
        foreach ($match[1] as  $v) {
        db('article_img')->insert(['art_id'=>$art_id,'src'=>$v]);
        }
       
      //发帖统计(个人)
      $count=db('user')->find($sid);
      $count=$count['ft_count']+1;
      db('user')->where('id',$sid)->setField('ft_count', $count);
       //发帖统计(板块)
      $topic=db('session')->find($ses_id);
      $topic=$topic['ft_topic']+1;
      db('session')->where('id',$ses_id)->setField('ft_topic', $topic);



      if ($res>0) {
             $this->success('恭喜发帖成功',"Content/index?art_id=".$art_id);
       }else{
            $this->error('未知错误发生');
       }
      
       
    
     
    }
    //删除文章页
    public function del()
    {
      $art_id=input('art_id');
      $res=db('article')->where('id',$art_id)->delete();
      if ($res>0) {
        $this->success('删除成功',"Index/index");
      }
    }

    //文章的回复
    public function reply ()
    {
       if (empty(input('session.user.id'))) {
        $this->error('登陆后才能执行操作哦','/index/User/login');
      }
        $art_id=input('aid');
        // dump($art_id);exit;
        $content=input('content');
        $uid=input('uid');
        $sid=input('sid');

       if (empty($content)) {
         $this->error('内容不能为空');
       }
        // dump($sid);exit;
        $data=["art_id"=>$art_id,"ses_id"=>$sid,"use_id"=>$uid,'time'=>date('Y-m-d H:i:s'),"content"=>$content];
          $res=db('reply')->insert($data);
          // 文章回复的数量统计
          $count=db('article')->find($art_id);
          $count=$count['count']+1;
          db('article')->where('id',$art_id)->setField('count', $count);
           // 板块回复的数量统计
           // 
           // 
           // 
          //自己所有的回复统计
          $num=db('user')->find($uid);
          // dump($num);exit;
          $num=$num['ht_count']+1;
          db('user')->where('id',$uid)->setField('ht_count', $num);
          if ($res>0) {
             $this->success('评论成功',"Content/index?art_id=".$art_id);
       }else{
            $this->error('未知错误发生');
       }
    }

    //删除评论
    public function delreply()
    {
      $id=input('id');
      $aid=input('aid');
      $res=db('reply')->where('id',$id)->delete();
      $count=db('article')->find($aid);
      $count=$count['count']-1;
      db('article')->where('id',$aid)->setField('count', $count);
      if ($res>0) {
        $this->success('删除成功');
      }
    }

}

